package com.apesource.mallproject.controller;

import com.apesource.mallproject.pojo.Advertising;
import com.apesource.mallproject.service.IAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(allowCredentials = "true", maxAge = 3600)
public class AdController {
    @Autowired
    IAdService iAdService;

    @RequestMapping("/Ad")
    public List<Advertising> queryAll() {
        return iAdService.queryAll();
    }


    @RequestMapping("/insertAd")
    public void insertAd(@RequestParam Map<String, String> adMap) {
        Integer categoryId = Integer.parseInt(adMap.get("categoryId"));
        String title = adMap.get("title");
        String url = adMap.get("url");
        String imgurl = adMap.get("uploadPicUrl");
        Integer status = Integer.parseInt(adMap.get("status"));
        Integer sortOrder = Integer.parseInt(adMap.get("sortOrder"));
        iAdService.insertAd(new Advertising(categoryId, title, url, imgurl, status, sortOrder));
    }

    @RequestMapping("/uploadAd")
    public void updateAd(@RequestParam Map<String, String> updateMap) {
        Integer categoryId = Integer.parseInt(updateMap.get("categoryId"));
        Integer Id = Integer.parseInt(updateMap.get("id"));
        String title = updateMap.get("title");
        String url = updateMap.get("url");
        String imgurl = updateMap.get("uploadPicUrl");
        Integer status = Integer.parseInt(updateMap.get("status"));
        Integer sortOrder = Integer.parseInt(updateMap.get("sortOrder"));
        iAdService.updateAd(new Advertising(Id, categoryId, title, url, imgurl, status, sortOrder));

    }

    //删除一个
    @RequestMapping("/delAd/{id}")
    public void delBrand(@PathVariable BigInteger id) {
        iAdService.delOne(id);
    }

    //批量删除
    @RequestMapping("/batchDelAd")
    public void batchDel(@RequestBody HashMap<String, String> idsMap) {
        String id = idsMap.get("ids");
        String[] ids = id.split(",");
        iAdService.batchDel(ids);
    }
}
